import {
  createRouter,
  createWebHashHistory
} from 'vue-router'
import Home from '../views/home/Home.vue';
import Login from '../views/login/Login.vue';
import Register from '../views/register/Register.vue';
const routes = [{
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    beforeEnter(to, from, next) {
      const isLogin = localStorage.isLogin;
      if(isLogin){
        next({name:'Home'});
      }else{
        next();
      }
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    beforeEnter(to, from, next) {
      const isLogin = localStorage.isLogin;
      if(isLogin){
        next({name:'Home'});
      }else{
        next();
      }
    }
  },
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
router.beforeEach((to, from, next) => {
  const isLogin = localStorage.isLogin;
  if (isLogin || to.name === 'Login' || to.name==="Register") {
    next();
  } else {
    next({
      name: 'Login'
    })
  }
})
export default router